package websocket;

import java.io.IOException;
import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;

import model.Product;

import org.apache.catalina.websocket.MessageInbound;
import org.apache.catalina.websocket.StreamInbound;
import org.apache.catalina.websocket.WebSocketServlet;
import org.apache.catalina.websocket.WsOutbound;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

@WebServlet("/testWebsocket")
public class TestWebSocket extends WebSocketServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected StreamInbound createWebSocketInbound(String string,
			HttpServletRequest hsr) {
		return new MessageInbound() {

			@Override
			protected void onBinaryMessage(ByteBuffer bb) throws IOException {

			}

			@SuppressWarnings("unchecked")
			@Override
			protected void onTextMessage(CharBuffer cb) throws IOException {
				WsOutbound outbound = getWsOutbound();
				Long orderNr = new Long(cb.toString());
				JSONArray array = new JSONArray();				
				if(orderNr != null){
					List<Product> products = getProductList();
					for(Product p : products){
						if(p.getOrderNr().equals(orderNr)){
							JSONObject obj = new JSONObject();
							obj.put("artikkel", p.getArticle());
							obj.put("nimetus", p.getName());
							obj.put("hind", p.getPrice());
							array.add(obj);							
						}
					}
					
				}
				
				CharBuffer b = CharBuffer.wrap(JSONValue.toJSONString(array));
				outbound.writeTextMessage(b);
				
			}
		};
	}
	
	private List<Product> getProductList(){
		List<Product> products = new ArrayList<Product>();
		products.add(new Product(653214L, "Telekas", new BigDecimal("500"), 100L));
		products.add(new Product(653214L, "DVD mängija", new BigDecimal("150"), 100L));
		products.add(new Product(65322L, "Raadio", new BigDecimal("25.50"), 101L));
		products.add(new Product(65324L, "Arvuti", new BigDecimal("600"), 102L));
		products.add(new Product(653221L, "Telefon", new BigDecimal("500"), 103L));
		products.add(new Product(65320L, "Aku", new BigDecimal("50"), 104L));
		
		return products;
	}
	
	@SuppressWarnings("unchecked")
	private void broadCast(){
		JSONArray array = new JSONArray();
		Random rnd = new Random();
		
		JSONObject obj = new JSONObject();
		obj.put("date", "01.03.2013");
		obj.put("amount", rnd.nextInt(500));
		array.add(obj);			
		
		JSONObject obj2 = new JSONObject();
		obj2.put("date", "02.03.2013");
		obj2.put("amount", rnd.nextInt(500));
		array.add(obj2);		
		
		JSONObject obj3 = new JSONObject();
		obj3.put("date", "03.03.2013");
		obj3.put("amount", rnd.nextInt(500));
		array.add(obj3);	
		
		JSONObject obj4 = new JSONObject();
		obj4.put("date", "04.03.2013");
		obj4.put("amount", rnd.nextInt(500));
		array.add(obj4);	

		//OrdersPerDayConnectionPool.broadcastMessage(JSONValue.toJSONString(array), null);
	}

}